//
//  ShoppingHotTableViewCell.m
//  quxiangtao
//
//  Created by xbmios on 2018/6/26.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "ShoppingHotTableViewCell.h"
#import "ShoppingHotContentTableViewCell.h"

static NSString *hotGoodsCell = @"hotGoodsCell";

@interface ShoppingHotTableViewCell ()<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UIView *bg;

@property (strong, nonatomic) UIView *headerView;
@property (strong, nonatomic) UITableView *hotGoodsTableView;
@property (strong, nonatomic) UIView *loadMoreView;
@property (strong, nonatomic) UIImageView *smallBackIV;
@property (strong, nonatomic) UILabel *loadMoreL;

// 用于标记在价格是选中状态下，显示不同的图片  YES 表示价格升序  NO表示价格降序
@property (assign, nonatomic) BOOL priceIsSelected;

@property (strong, nonatomic) UIButton *selectedBtn;

@end

@implementation ShoppingHotTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.priceIsSelected = YES;
        
        
        self.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor clearColor];
        
        [self.contentView addSubview:self.bg];
        [self.bg addSubview:self.headerView];
        [self.bg addSubview:self.hotGoodsTableView];
        [self.bg addSubview:self.loadMoreView];
        [self.loadMoreView addSubview:self.smallBackIV];
        [self.loadMoreView addSubview:self.loadMoreL];
        
        [self setupUI];
        
    }
    
    return self;
    
}

#pragma mark -更多商品点击事件
- (void)loadMoreGestureClick:(UIGestureRecognizer *)recognizer {
    
    NSLog(@"更多商品点击事件");
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return jisuan(225);

}


#pragma mark -delegate、datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ShoppingHotContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:hotGoodsCell];
    
    if (!cell) {
        
        cell = [[ShoppingHotContentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:hotGoodsCell];
        
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog(@"热销商品点击事件");
    
}

#pragma mark -分栏按钮点击事件
- (void)btnClick:(UIButton *)btn {
    
    if (btn!= self.selectedBtn) {
    
        self.selectedBtn.selected = NO;
        btn.selected = YES;
        self.selectedBtn = btn;
    
    }else{
        
        self.selectedBtn.selected = YES;
        
    }

    if (btn.tag == 0) {
        
        NSLog(@"综合");
        self.priceIsSelected = YES;
        
        // 综合数据请求
        
    }else if (btn.tag == 1) {
        
        NSLog(@"销量");
        self.priceIsSelected = YES;
        
        // 销量数据请求
    
    }else if (btn.tag == 2) {

        if (self.priceIsSelected) {
            
            NSLog(@"升序");
            
            [btn setImage:[UIImage imageNamed:@"priceUp"] forState:UIControlStateSelected];
            
            self.priceIsSelected = NO;
            
            // 价格升序数据请求
            
    
        }else {
            
            NSLog(@"降序");
            
            [btn setImage:[UIImage imageNamed:@"priceDown"] forState:UIControlStateSelected];
            
            self.priceIsSelected = YES;
            
            // 价格降序数据请求
        }

    }else  {
        
        NSLog(@"筛选");
        self.priceIsSelected = YES;
        
        // 筛选数据请求
        
    }

}

- (void)setupUI {
    
    __weak typeof(self) weakSelf = self;
    [self.bg mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.contentView.mas_left).with.offset(Margin);
        make.right.mas_equalTo(weakSelf.contentView.mas_right).with.offset(-Margin);
        make.top.mas_equalTo(weakSelf.contentView.mas_top);
        make.bottom.mas_equalTo(weakSelf.contentView.mas_bottom);
        
    }];
    
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.bg.mas_left);
        make.right.mas_equalTo(weakSelf.bg.mas_right);
        make.top.mas_equalTo(weakSelf.bg.mas_top);
        make.height.mas_equalTo(jisuan(60));
    
    }];
    
    [self.hotGoodsTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.bg.mas_left);
        make.right.mas_equalTo(weakSelf.bg.mas_right);
        make.top.mas_equalTo(weakSelf.headerView.mas_bottom).with.offset(jisuan(1));
        make.bottom.mas_equalTo(weakSelf.bg.mas_bottom).with.offset(-jisuan(61));
        
    }];
    
    [self.loadMoreView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.bg.mas_left);
        make.right.mas_equalTo(weakSelf.bg.mas_right);
        make.bottom.mas_equalTo(weakSelf.bg.mas_bottom);
        make.height.mas_equalTo(jisuan(60));
   
    }];
    
    [self.loadMoreL mas_makeConstraints:^(MASConstraintMaker *make) {

        make.centerY.mas_equalTo(weakSelf.loadMoreView.mas_centerY);
        make.centerX.mas_equalTo(weakSelf.loadMoreView.mas_centerX).with.offset(-jisuan(10));
        
    }];
    [self.smallBackIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.loadMoreL.mas_right).with.offset(jisuan(10));
        make.centerY.mas_equalTo(weakSelf.loadMoreView.mas_centerY);
        make.width.mas_equalTo(jisuan(11));
        make.height.mas_equalTo(jisuan(20));
    }];
    

    [self createWithtitle:@"综合" image:nil withTag:0];
    [self createWithtitle:@"销量" image:nil withTag:1];
    [self createWithtitle:@"价格" image:[UIImage imageNamed:@"priceNormal"] withTag:2];
    [self createWithtitle:@"优惠" image:nil withTag:3];
    
}

- (void)createWithtitle:(NSString *)name image:(UIImage *)image withTag:(NSInteger)tag {
    
    UIButton *btn = [[UIButton alloc] init];
    btn.titleLabel.font = Font(24);
    btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [btn setTitle:name forState:UIControlStateNormal];
    [btn setTitleColor:Color333333 forState:UIControlStateNormal];
    [btn setTitleColor:Colorff0000 forState:UIControlStateSelected];
    
    btn.tag = tag;
    
    CGFloat width = jisuan(80);
    CGFloat margin = (WIDTH - SmallMargin * 2 - Margin * 2 - width * 4) / 3.0;
    CGFloat x = SmallMargin + (width + margin) * tag;
    btn.frame = CGRectMake(x, 0, width, jisuan(60));
    [self.headerView addSubview:btn];
    
    if (image) {
        
        [btn setImage:image forState:UIControlStateNormal];
    }
    
    if (tag == 0) {
        
        // 设置“综合”为默认选中状态
        btn.selected = YES;
        self.selectedBtn = btn;
        
    }else {
        
        btn.selected = NO;
        
    }
    
    
    if (tag == 0) {
        
        // 综合
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

    }else if (tag == 3) {

        // 筛选
        btn.titleEdgeInsets = UIEdgeInsetsMake(0, -btn.imageView.frame.size.width - btn.frame.size.width + btn.titleLabel.frame.size.width + jisuan(15), 0, 0);
        btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -btn.titleLabel.frame.size.width - btn.frame.size.width +  jisuan(20));
    
    }else if (tag == 2) {
        
        // 价格
        
        btn.titleEdgeInsets = UIEdgeInsetsMake(0, -btn.imageView.frame.size.width - btn.frame.size.width + btn.titleLabel.frame.size.width + jisuan(15), 0, 0);
        btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -btn.titleLabel.frame.size.width - btn.frame.size.width + jisuan(20));

    }

    
    if (tag == 2) {
        
        // 价格
        [btn setImage:[UIImage imageNamed:@"priceUp"] forState:UIControlStateSelected];
        
    }else if (tag == 3) {
        
        // 筛选
//        [btn setImage:[UIImage imageNamed:@"filterSelected"] forState:UIControlStateSelected];
        
    }
    
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
}

#pragma mark -lazy
- (UIView *)bg {
    
    if (!_bg) {
        
        _bg = [[UIView alloc] init];
        _bg.backgroundColor = Colordddddd;
        
        _bg.frame = CGRectMake(Margin, 0, SCREEN_WIDTH - Margin * 2, jisuan(1020));
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:_bg.bounds byRoundingCorners:UIRectCornerBottomRight | UIRectCornerBottomLeft cornerRadii:CGSizeMake(jisuan(10), jisuan(10))];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = _bg.bounds;
        //赋值
        maskLayer.path = path.CGPath;
        _bg.layer.mask = maskLayer;
    
    }
    
    return _bg;
}

- (UIView *)headerView {
    
    if (!_headerView) {
        
        _headerView = [[UIView alloc] init];
        
        _headerView.backgroundColor = WhiteColor;
    }
    
    return _headerView;

}

- (UITableView *)hotGoodsTableView {
   
    if (!_hotGoodsTableView) {
        
        _hotGoodsTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        
        _hotGoodsTableView.backgroundColor = WhiteColor;
        
        _hotGoodsTableView.delegate = self;
        _hotGoodsTableView.dataSource = self;
        
        _hotGoodsTableView.scrollEnabled = NO;
        
        _hotGoodsTableView.showsVerticalScrollIndicator = NO;
        _hotGoodsTableView.showsHorizontalScrollIndicator = NO;
        
        _hotGoodsTableView.estimatedRowHeight = 0;
        _hotGoodsTableView.estimatedSectionFooterHeight = 0;
        _hotGoodsTableView.estimatedSectionHeaderHeight = 0;
        
        _hotGoodsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        if (@available(iOS 11.0, *)) {
            
            _hotGoodsTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    
    }
    
    return _hotGoodsTableView;

}

- (UIView *)loadMoreView {
    
    if (!_loadMoreView) {
        
        _loadMoreView = [[UIView alloc] init];
        _loadMoreView.backgroundColor = WhiteColor;
    }
    
    return _loadMoreView;

}

- (UIImageView *)smallBackIV {
    
    if (!_smallBackIV) {
        
        _smallBackIV = [[UIImageView alloc] init];
        _smallBackIV.image = [UIImage imageNamed:@"smallBackBg1"];
        _smallBackIV.contentMode = UIViewContentModeScaleAspectFit;
        
    }
    
    return _smallBackIV;
}

- (UILabel *)loadMoreL {
    
    if (!_loadMoreL) {
        
        _loadMoreL = [[UILabel alloc] init];
        _loadMoreL.backgroundColor = WhiteColor;
        
        _loadMoreL.text = @"更多商品";
        _loadMoreL.textColor = Color1f89ff;
        _loadMoreL.font = Font(20);
        _loadMoreL.textAlignment = NSTextAlignmentRight;
        
        _loadMoreL.userInteractionEnabled = YES;
        UITapGestureRecognizer *loadMoreGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(loadMoreGestureClick:)];
        [_loadMoreL addGestureRecognizer:loadMoreGesture];
        
    }
    
    return _loadMoreL;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
